<template>
  <div class="user-page">
    <div class="user-page-head">
      <div class="top">
        <van-image round fit="cover" :src="userInfo?.avatar" />
        <div class="name">
          <p>{{ userInfo?.account }}</p>
          <p><van-icon name="edit" /></p>
        </div>
      </div>
      <van-row>
        <van-col span="6">
          <p>{{ userInfo?.collectionNumber }}</p>
          <p>收藏</p>
        </van-col>
        <van-col span="6">
          <p>{{ userInfo?.likeNumber }}</p>
          <p>关注</p>
        </van-col>
        <van-col span="6">
          <p>{{ userInfo?.score }}</p>
          <p>积分</p>
        </van-col>
        <van-col span="6">
          <p>{{ userInfo?.couponNumber }}</p>
          <p>优惠券</p>
        </van-col>
      </van-row>
    </div>
    <div class="user-page-order">
      <div class="head">
        <h3>药品订单</h3>
        <router-link to="/order"
          >全部订单 <van-icon name="arrow"
        /></router-link>
      </div>
      <van-row>
        <van-col span="6">
          <van-badge
            :content="userInfo?.orderInfo.paidNumber"
            :show-zero="false"
          >
            <cp-icon name="user-paid" />
          </van-badge>
          <p>待付款</p>
        </van-col>
        <van-col span="6">
          <van-badge
            :content="userInfo?.orderInfo.receivedNumber"
            :show-zero="false"
          >
            <cp-icon name="user-shipped" />
          </van-badge>
          <p>待发货</p>
        </van-col>
        <van-col span="6">
          <van-badge
            :content="userInfo?.orderInfo.shippedNumber"
            :show-zero="false"
          >
            <cp-icon name="user-received" />
          </van-badge>
          <p>待收货</p>
        </van-col>
        <van-col span="6">
          <van-badge
            :content="userInfo?.orderInfo.finishedNumber"
            :show-zero="false"
          >
            <cp-icon name="user-finished" />
          </van-badge>
          <p>已完成</p>
        </van-col>
      </van-row>
    </div>
    <div class="user-page-group">
      <h3>快捷工具</h3>
      <van-cell
        v-for="(item, index) in tools"
        :key="index"
        :title="item.label"
        is-link
        :border="false"
        :to="item.path"
      >
        <template #icon>
          <cp-icon :name="`user-tool-0${index + 1}`" />
        </template>
      </van-cell>
    </div>
    <a class="logout" href="javascript:;" @click="logout">退出登录</a>
  </div>
</template>

<script setup lang="ts">
// 实现一个页面的思路
// 1、根据原型图设计图分析页面的布局和交互
// 2、根据页面结构完成dom开发 从上到下 从外到内
// 3、根据页面结构拆分组件
// 4、根据设计稿的样式标注还原实现样式 大小间距字体颜色。。。
// 5、找到当前页面渲染数据的接口
// 6、定义类型定义接口接口调用获取数据
// 7、数据绑定渲染
// 8、定义事件交互
import { getMyUser } from '@/services/user'
import type { IMyUserResData } from '@/types/user'
import { ref, onMounted } from 'vue'
import { showConfirmDialog } from 'vant'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
const userInfo = ref<IMyUserResData>()
const getUserInfo = async () => {
  const res = await getMyUser()
  userInfo.value = res.data
}

// 快捷工具渲染数据
// 非响应式数据
const tools = [
  {
    label: '我的问诊',
    path: '/user/consult'
  },
  {
    label: '我的处方',
    path: '/'
  },
  {
    label: '家庭档案',
    path: '/user/patient'
  },
  {
    label: '地址管理',
    path: '/user/address'
  },
  {
    label: '我的评价',
    path: '/'
  },
  {
    label: '官方客服',
    path: '/'
  },
  {
    label: '设置',
    path: '/'
  }
]

// 退出登录
// 1、声明退出登录的方法
// 2、二次确认
// 3、清空token & 用户信息
// 4、跳转到登录页
const store = useUserStore()
const router = useRouter()
const logout = async () => {
  await showConfirmDialog({
    title: '温馨提示',
    message: '您确认要退出优医问诊么？'
  })
  store.clearUserInfo()
  console.log('清空token & 用户信息')
  router.push('/login')
}

onMounted(() => {
  getUserInfo()
})
</script>

<style lang="scss" scoped>
.user-page {
  background-color: var(--cp-bg);
  min-height: calc(100vh - 50px);
  padding: 0 15px 65px;
  // 头部
  &-head {
    height: 200px;
    background: linear-gradient(
      180deg,
      rgba(44, 181, 165, 0.46),
      rgba(44, 181, 165, 0)
    );
    margin: 0 -15px;
    padding: 0 15px;
    .top {
      display: flex;
      padding-top: 50px;
      align-items: center;
      .van-image {
        width: 70px;
        height: 70px;
      }
      .name {
        padding-left: 10px;
        p {
          &:first-child {
            font-size: 18px;
            font-weight: 500;
          }
          &:last-child {
            margin-top: 10px;
            color: var(--cp-primary);
            font-size: 16px;
          }
        }
      }
    }
    .van-row {
      margin: 0 -15px;
      padding-top: 15px;
      p {
        text-align: center;
        &:first-child {
          font-size: 18px;
          font-weight: 500;
        }
        &:last-child {
          color: var(--cp-dark);
          font-size: 12px;
          padding-top: 4px;
        }
      }
    }
  }
  // 订单
  &-order {
    background-color: #fff;
    border-radius: 8px;
    margin-bottom: 15px;
    padding-bottom: 15px;
    .head {
      display: flex;
      justify-content: space-between;
      line-height: 50px;
      padding: 0 15px;
      a {
        color: var(--cp-tip);
      }
    }
    .van-col {
      text-align: center;
      .cp-icon {
        font-size: 28px;
      }
      p {
        font-size: 12px;
        padding-top: 4px;
      }
    }
  }
  // 分组
  &-group {
    background-color: #fff;
    border-radius: 8px;
    overflow: hidden;
    h3 {
      padding-left: 16px;
      line-height: 44px;
    }
    .van-cell {
      align-items: center;
    }
    .cp-icon {
      font-size: 17px;
      margin-right: 10px;
    }
  }
  .logout {
    display: block;
    margin: 20px auto;
    width: 100px;
    text-align: center;
    color: var(--cp-price);
  }
}
</style>
